<?php

use support\Cache;
use think\facade\Db;

if ( ! function_exists('blog_date_document')) {
//文章按日归档统计
    function blog_date_document($arrData)
    {
        $documents = array();
        foreach ($arrData as $index => $value) {
            $archivesTime = date("Y-m-d", $value['create_time']);
            if (empty($documents[$archivesTime])) {
                $documents[$archivesTime] = 1;
            } else {
                $documents[$archivesTime] += 1;
            }
        }

        return $documents;
    }
}
if ( ! function_exists('blog_month_document')) {
//文章归档按月
    function blog_month_document($arrData)
    {
        $documents = array();
        foreach ($arrData as $index => $value) {
            $value['create_date']       = date("d", $value['create_time']);
            $archivesTime               = date("Y-m", $value['create_time']);
            $documents[$archivesTime][] = $value;
        }

        return $documents;
    }
}
if ( ! function_exists('blog_year_document')) {
//文章归档按年
    function blog_year_document($arrData)
    {
        $documents = array();
        foreach ($arrData as $index => $value) {
            $dateArray                                        = explode("-", $index);
            $archivesTime                                     = $dateArray[0];
            $documents[$archivesTime][$dateArray[1]]['list']  = $value;
            $documents[$archivesTime][$dateArray[1]]['count'] = count($value);
        }

        return $documents;
    }
}

if ( ! function_exists('get_blog_config')) {
//读取系统配置
    function get_blog_config($name, $key = '')
    {
        $config = [];
        if (Cache::get('huiblog_config'.$name)) {
            $config = Cache::get('huiblog_config'.$name);
        } else {
            $conf = Db::table('blog_config')->where('name', $name)->find();
            if ($conf['content']) {
                $config = unserialize($conf['content']);
            }
            Cache::set('blog_config'.$name, $config);
        }
        if ($key == '') {
            return $config;
        } else {
            if ($config[$key]) {
                return $config[$key];
            }
        }
    }
}

if ( ! function_exists('list_to_tree')) {
    //菜单转为父子菜单
    function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'list', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[$data[$pk]] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent                     = &$refer[$parentId];
                        $parent[$child][$data[$pk]] = &$list[$key];
                    }
                }
            }
        }

        return $tree;
    }
}

if ( ! function_exists('get_random_str')) {
    /**
     * 产生一个指定长度的随机字符串,并返回给用户
     *
     * @param integer $len 产生字符串的长度
     *
     * @return string 随机字符串
     */
    function get_random_str($len = 6)
    {
        $chars    = array(
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
            "g",
            "h",
            "i",
            "j",
            "k",
            "l",
            "m",
            "n",
            "o",
            "p",
            "q",
            "r",
            "s",
            "t",
            "u",
            "v",
            "w",
            "x",
            "y",
            "z",
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z",
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9"
        );
        $charsLen = count($chars) - 1;
        // 将数组打乱
        shuffle($chars);
        $output = "";
        for ($i = 0; $i < $len; $i++) {
            $output .= $chars[mt_rand(0, $charsLen)];
        }

        return $output;
    }
}

if ( ! function_exists('is_mobile')) {
//判断你手机访问
    function is_mobile()
    {
        static $is_mobile;

        if (isset($is_mobile)) {
            return $is_mobile;
        }

        if (empty($_SERVER['HTTP_USER_AGENT'])) {
            $is_mobile = false;
        } else {
            if (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false || strpos($_SERVER['HTTP_USER_AGENT'],
                    'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'],
                    'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'],
                    'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'],
                    'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false) {
                $is_mobile = true;
            } else {
                $is_mobile = false;
            }
        }

        return $is_mobile;
    }
}
